﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ECGadmin.Common;

namespace ECGadmin.DAL
{
    public partial class ProviderDb
    {
        public static List<ProviderListDto> GetAllProvider(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ProviderListDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Search provider in admin 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalPage"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static List<ProviderListDto> GetAllProviderByKeyWordAdmin(int page, int itemsPerPage, ref int totalPage, string keyword)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetAllByKeyword");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@Condition", keyword));
                SqlParameter outTotalpage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                outTotalpage.Direction = ParameterDirection.Output;
                var result = dao.GetDTOList<ProviderListDto>(ref command);
                totalPage = int.Parse(outTotalpage.Value.ToString() == "" ? "0" : outTotalpage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        public static List<ProviderListByLetterDto> GetAllProviderByLetter(string categoryId, string letter)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_ByLetter");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@CateoryId", categoryId));
                command.Parameters.Add(dao.CreateParameter("@Letter", letter));
                var result = dao.GetDTOList<ProviderListByLetterDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        public static List<ProviderListCateHomeDto> GetAllProviderByKeyWordAdmin(string Category)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetbyCategoryHome");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@CategoryId", Category));
                var result = dao.GetDTOList<ProviderListCateHomeDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<ProviderListHotCateDto> GetHotByCate(string Category)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetHotByCate");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@CategoryId", Category));
                var result = dao.GetDTOList<ProviderListHotCateDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static int GetTotalProviderBycate(string Category)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetTotalByCate");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@CateoryId", Category));
                var result = dao.GetSingleDTO<ProviderCoutDto>(ref command);
                return result.total;
            }
            finally
            {
                command.Connection.Close();
            }
        }
        public static List<ProviderListHotCateDto> GetHotByCateList(string Category)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Provider_GetHotByCateList");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@CategoryId", Category));
                var result = dao.GetDTOList<ProviderListHotCateDto>(ref command);
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static string BuildListKeywordV2(string keyword)
        {
            var result = string.Empty;
            var words = keyword.Split(new[] { '%', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); //,StringSplitOptions.RemoveEmptyEntries

            var lastKeyword = "";
            lastKeyword += "ISABOUT (\"" + keyword + "\" WEIGHT(0.9)";
            for (int i = 0; i < words.Count; i++)
            {
                lastKeyword += "," + words[i] + " WEIGHT(0.1)";
            }
            lastKeyword += ")";
            result = lastKeyword;
            return result;
        }
    }
}
